package vip.xiaonuo.device.modular.service.impl;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.device.modular.entity.BizGoods;
import vip.xiaonuo.device.modular.mapper.GoodsMapper;
import vip.xiaonuo.device.vo.CabinetScreenGoodsVO;
import vip.xiaonuo.device.vo.Result;

import javax.annotation.Resource;

/**
 * @author 墨塵
 * @since 2023/5/18
 */
@Service("deviceGoodsAddService")
@Transactional
public class DeviceGoodsAddServiceImpl extends DeviceBaseService{

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    protected Integer getReqType() {
        return null;
    }

    @Override
    protected Boolean getLogFlag() {
        return false;
    }

    @Override
    protected Result handle(JSONObject param) {
        String data = param.getString("data");
        if(StringUtils.isBlank(data)){
            return Result.fail("参数缺失,请传入正确的参数值data");
        }
        BizGoods entity = JSONObject.parseObject(data,BizGoods.class);
        if(StringUtils.isBlank(entity.getRfidNum())){
            return Result.fail("RFID号确实，请传入正确的参数值");
        }
        //判定rfid是否存在
        BizGoods info = goodsMapper.getInfoByRfid(entity.getRfidNum().trim());
        if(null!=info){
            return Result.fail("RFID号["+entity.getRfidNum()+"]已存在，请勿重复新增");
        }
        entity.setGoodsType(1);
        entity.setRfidFlag(1);
        //ml
        if (entity.getReagentUnit().equals("0")) {
            entity.setReagentInitVolume(entity.getReagentInitNetAmount());
            entity.setReagentInitWeight(NumberUtil.mul(entity.getReagentInitNetAmount(), entity.getReagentDensity()));
        }
        //g
        if (entity.getReagentUnit().equals("1")) {
            entity.setReagentInitVolume(NumberUtil.div(entity.getReagentInitNetAmount(), entity.getReagentDensity(), 2));
            entity.setReagentInitWeight(entity.getReagentInitNetAmount());
        }
        //可用 = 初始净量
        entity.setReagentAvlWeight(entity.getReagentInitWeight());
        entity.setReagentAvlVolume(entity.getReagentInitVolume());
        goodsMapper.insert(entity);
        return Result.ok(entity);
    }
}
